package com.alibaba.csp.sentinel.dashboard.rule.apollo;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import org.springframework.stereotype.Component;


/**
 * @ClassName ParamFlowRuleApolloProvider
 * @Description TODO
 * @Author zhouchao
 * @Date 2020/2/17 20:24
 * @Version 1.0
 */
@Component("paramFlowRuleApolloProvider")
public class ParamFlowRuleApolloProvider extends AbstractApolloCommonService implements
        DynamicRuleProvider<List<ParamFlowRuleEntity>> {

    @Override
    public List<ParamFlowRuleEntity> getRules(String appName) throws Exception {
        List<ParamFlowRule> rules = apolloCommonService.getRules(appName, paramFlowDataIdSuffix, ParamFlowRule.class);
        List<ParamFlowRuleEntity> paramFlowRuleEntitys = rules.stream().map(r -> {
            ParamFlowRuleEntity paramFlowRuleEntity = new ParamFlowRuleEntity();
            paramFlowRuleEntity.setApp(appName);
            paramFlowRuleEntity.setRule(r);
            return paramFlowRuleEntity;
        }).collect(Collectors.toList());
        return paramFlowRuleEntitys;
    }
}
